/*
 * Copyright (C) 2009 Dimitrios C. Michalakos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.restsql.db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * MySQLForeignKeyInfo can be used to get information about the relations of a table.
 * @author dimitris@jmike.gr
 */
public class MySQLForeignKeyInfo extends GenericForeignKeyInfo implements ForeignKeyInfo {

    /**
     * Constructs a new MySQLForeignKeyInfo.
     * @param db the parent database.
     * @throws java.sql.SQLException
     */
    public MySQLForeignKeyInfo(Database db) throws SQLException {
        /* set resultset */
        final String sql = "SELECT `CONSTRAINT_NAME`, `TABLE_NAME`, `COLUMN_NAME`, `REFERENCED_TABLE_NAME`, `REFERENCED_COLUMN_NAME` FROM `information_schema`.`KEY_COLUMN_USAGE` WHERE `CONSTRAINT_SCHEMA` = ? AND `REFERENCED_TABLE_SCHEMA` IS NOT NULL ORDER BY `CONSTRAINT_NAME` ASC, `POSITION_IN_UNIQUE_CONSTRAINT` ASC;";
        PreparedStatement ps = db.getPreparedStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);//Create prepared statement
        ps.setString(1, db.getSchemaName());//set schema
        ResultSet rs = ps.executeQuery();
        /* set index */
        rs.beforeFirst();//reset rs cursor
        int i = 0;//initialize internal id
        while (rs.next()) {//iterate rs
        //index.put(rs.getString("CONSTRAINT_NAME"), new ArrayList<Integer>());//put 'foreign key name, empty list of internal ids' to index
            i++;//increase internal id (step 1)
        }
        /* set foreign key's columns and tables */
        table1 = new String[i];
        column1 = new String[i];
        table2 = new String[i];
        column2 = new String[i];
        rs.beforeFirst();//reset rs cursor
        i = 0;//reset internal id
        while (rs.next()) {//iterate rs again
            table1[i] = rs.getString("TABLE_NAME");
            column1[i] = rs.getString("COLUMN_NAME");
            table2[i] = rs.getString("REFERENCED_TABLE_NAME");
            column2[i] = rs.getString("REFERENCED_COLUMN_NAME");
            final String key = rs.getString("CONSTRAINT_NAME");
        //index.get(key).add(Integer.valueOf(i));//append internal id to this foreign key
        //relations.put(table1[i] + "-" + table2[i], key);//put 'table1-table2, foreign key name' to the relations
            i++;//increase internal id (step 1)
        }
        /* close resultset */
        rs.close();
        ps.close();
    }
}
